<!DOCTYPE html>
<html>
<head>
    <title>医院修改</title>
    {% include 'admin/common/header.html' %}
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <table lay-filter="demo" class="layui-table" style="width: auto; margin: 10px 10px 10px 10px;">
            <thead>
            <tr>
                <th data-field="0" data-key="1-0-0" data-unresize="true" class=" layui-table-col-special">
                    <div class="layui-table-cell laytable-cell-1-0-0 laytable-cell-checkbox">
                        <input type="checkbox" lay-skin="primary" lay-filter="layTableAllChoose">
                    </div>
                </th>
                <th>样本名称</th>
                <th width="280">身份证</th>
                <th width="100">姓名</th>
                <th width="100">年龄</th>
                <th width="100">性别</th>
                <th width="150">检查号</th>
                <th width="150">检查时间</th>
                <th>ct值</th>
                <th>错误理由</th>
            </tr>
            </thead>
            <tbody>
            {% for record in record_list %}
            <tr data-id="{{ record.id }}">
                <td class="layui-table-col-special">
                    <div class="layui-table-cell laytable-cell-1-0-0 laytable-cell-checkbox">
                        <input lay-filter="layTableChoose" type="checkbox" name="ids[]" lay-skin="primary" value="{{ record.id }}">
                    </div>
                </td>
                <td>{{ record.name }}</td>
                <td><input type="text" value="" name="idno[]" autocomplete="off" class="layui-input" style="display: inline-block;width: 80%;">
                    <a class="layui-btn layui-btn-sm id-search" data-id="{{ record.id }}"  data-title="查询">查询</a>
                </td>
                <td><input type="text" value="" name="username[]" autocomplete="off" class="layui-input" style="display: inline-block;"></td>
                <td><input type="text" value="" autocomplete="off" class="layui-input year" style="display: inline-block;"></td>
                <td><select name="sex[]">
                    <option value="">请选择</option>
                    <option value="1">男</option>
                    <option value="2">女</option>
                </select></td>
                <td><input type="text" value="" name="case_no[]" autocomplete="off" class="layui-input" style="display: inline-block;"></td>
                <td><input type="text" value="" name="test_time[]" autocomplete="off" class="layui-input date" style="display: inline-block;"></td>
                <td>
                 mir:&nbsp;<div class="mir_data" style="display: inline;" data-states="{{ record.states }}">{% for m in record.mir %}&nbsp;&nbsp;<span>{{ m.ct }}</span>{% endfor %}</div>
                    <br>
                 u6:<div class="u6_data" style="display: inline;" data-states="{{ record.states }}">&nbsp;{% for u in record.u6 %}&nbsp;&nbsp;<span>{{ u.ct }}</span>{% endfor %}</div>
                </td>
                <td>{{ record.exception }}</td>
            </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="record-update">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{% include 'admin/common/footer.html' %}
<script>
    function IdCard(IdCard, type) {
        if (type === 1) {
            //获取出生日期
            let birthday = IdCard.substring(6, 10) + "-" + IdCard.substring(10, 12) + "-" + IdCard.substring(12, 14)
            return birthday
        }
        if (type === 2) {
            //获取性别
            if (parseInt(IdCard.substr(16, 1)) % 2 === 1) {
                return "1"
            } else {
                return "2"
            }
        }
        if (type === 3) {
            //获取年龄
            var ageDate = new Date()
            var month = ageDate.getMonth() + 1
            var day = ageDate.getDate()
            var age = ageDate.getFullYear() - IdCard.substring(6, 10) - 1
            if (IdCard.substring(10, 12) < month || IdCard.substring(10, 12) === month && IdCard.substring(12, 14) <= day) {
                age++
            }
            if (age <= 0) {
                age = 1
            }
            return age
        }
    }

    function identityValidate(value) {
         let city={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",
        32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",
        44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",
        62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外 "};
        let tip = "";
        let pass = true;

        if (!value || !/^[1-9][0-9]{5}(19[0-9]{2}|200[0-9]|2010)(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])[0-9]{3}[0-9xX]$/i.test(value)) {
            tip = "身份证号格式错误";
            pass = false;
        } else if (!city[value.substr(0, 2)]) {
            tip = "地址编码错误";
            pass = false;
        } else {
            //18位身份证需要验证最后一位校验位
            if (value.length == 18) {
                value = value.split('');
                //∑(ai×Wi)(mod 11)
                //加权因子
                let factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
                //校验位
                let parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
                let sum = 0;
                let ai = 0;
                let wi = 0;
                for (var i = 0; i < 17; i++) {
                    ai = value[i];
                    wi = factor[i];
                    sum += ai * wi;
                }
                let last = parity[sum % 11];
                if (parity[sum % 11] != value[17]) {
                    tip = "校验位错误";
                    pass = false;
                }
            }
        }
        return pass;
    }
</script>
<script>
    layui.use(['form', 'jquery'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        var laydate = layui.laydate;

        laydate.render({
          elem: '.date'
          , type: 'datetime'
          ,format: 'yyyy-MM-dd HH:mm' //可任意组合
        });

        $(".mir_data").each(function (i,o){
            let span = $(o).find("span");
            let count = span.length;
            if (count != 4){
                $(o).css("color","red");
                $(o).parent().next().text('MIR数据格式不正确')
            }else {
                span.each(function (ii,oo){
                    let v = Number($(oo).val());
                    if (isNaN(v)){
                        $(oo).css("color","red");
                        $(oo).parent().parent().next().text('MIR数据格式不正确')
                    }else if (v >31){
                        $(oo).css("color","red");
                        $(oo).parent().parent().next().text('MIR ct值不正确')
                    }
                })
            }
        })

        $(".u6_data").each(function (i,o){
            let span = $(o).find("span");
            let count = span.length;
            if (count != 4){
                $(o).css("color","red");
                $(o).parent().next().text('U6数据格式不正确')
            }else {
                span.each(function (ii,oo){
                    let v = Number($(oo).text());
                    if (isNaN(v)){
                        $(oo).css("color","red");
                        $(oo).parent().parent().next().text('U6数据格式不正确')
                    }else if (v >34){
                        $(oo).css("color","red");
                        $(oo).parent().parent().next().text('U6 ct值不正确')
                    }
                })
            }
        })

        form.on('checkbox(layTableChoose)', function (data) {
            let tr = $(data.elem).parent().parent().parent();
            if (data.elem.checked) {
                if (tr.find('input[name="idno[]"]').attr('lay-verify') == undefined) {
                    tr.find('input[name="idno[]"]').attr('lay-verify', "required");
                }
                if (tr.find('input[name="username[]"]').attr('lay-verify') == undefined) {
                    tr.find('input[name="username[]"]').attr('lay-verify', "required");
                }
                if (tr.find('input[name="case_no[]"]').attr('lay-verify') == undefined) {
                    tr.find('input[name="case_no[]"]').attr('lay-verify', "required");
                }
                if (tr.find('input[name="test_time[]"]').attr('lay-verify') == undefined) {
                    tr.find('input[name="test_time[]"]').attr('lay-verify', "required");
                }
                if (tr.find('select[name="sex[]"]').attr('lay-verify') == undefined) {
                    tr.find('select[name="sex[]"]').attr('lay-verify', "required");
                }
            } else {
                tr.find('input[name="idno[]"]').removeAttr('lay-verify');
                tr.find('input[name="username[]"]').removeAttr('lay-verify');
                tr.find('input[name="case_no[]"]').removeAttr('lay-verify');
                tr.find('input[name="test_time[]"]').removeAttr('lay-verify');
                tr.find('select[name="sex[]"]').removeAttr('lay-verify');
            }
        })
        form.on('checkbox(layTableAllChoose)', function (data) {
            let b = $('tbody tr input[type="checkbox"]');
            if (data.elem.checked) {
                b.prop('checked', true);
                b.each(function (i, o) {
                    let tr = $(o).parent().parent().parent();
                    if (tr.find('input[name="idno[]"]').attr('lay-verify') == undefined) {
                        tr.find('input[name="idno[]"]').attr('lay-verify', "required");
                    }
                    if (tr.find('input[name="username[]"]').attr('lay-verify') == undefined) {
                        tr.find('input[name="username[]"]').attr('lay-verify', "required");
                    }
                    if (tr.find('input[name="case_no[]"]').attr('lay-verify') == undefined) {
                        tr.find('input[name="case_no[]"]').attr('lay-verify', "required");
                    }
                    if (tr.find('input[name="test_time[]"]').attr('lay-verify') == undefined) {
                        tr.find('input[name="test_time[]"]').attr('lay-verify', "required");
                    }
                    if (tr.find('select[name="sex[]"]').attr('lay-verify') == undefined) {
                        tr.find('select[name="sex[]"]').attr('lay-verify', "required");
                    }
                })
            } else {
                b.prop('checked', false);
                b.each(function (i, o) {
                    let tr = $(o).parent().parent().parent();
                    tr.find('input[name="idno[]"]').removeAttr('lay-verify');
                    tr.find('input[name="username[]"]').removeAttr('lay-verify');
                    tr.find('input[name="case_no[]"]').removeAttr('lay-verify');
                    tr.find('input[name="test_time[]"]').removeAttr('lay-verify');
                    tr.find('select[name="sex[]"]').removeAttr('lay-verify');
                })
            }
            form.render(); //重新渲染
        })
        form.on('submit(record-update)', function (data) {
            let ids = $("input[name='ids[]']:checked").map(function () {
                return $(this).val();
            }).get();
            var idno = [];
            var sex = [];
            var test_time = [];
            var case_no = [];
            var username = [];
            $("input[name='ids[]']:checked").each(function (i,o){
                let tr = $(o).parent().parent().parent();
                idno.push(tr.find("input[name='idno[]']").val());
                sex.push(tr.find("select[name='sex[]']").val());
                test_time.push(tr.find("input[name='test_time[]']").val());
                case_no.push(tr.find("input[name='case_no[]']").val());
                username.push(tr.find("input[name='username[]']").val());
            })
            {#let idno = $("input[name='idno[]']:checked").map(function () {
                return $(this).val();
            }).get();#}
            {#let sex = $("select[name='sex[]']:checked").map(function () {
                return $(this).val();
            }).get();#}
            {#let test_time = $("input[name='test_time[]:checked']").map(function () {
                return $(this).val();
            }).get();#}
            {#let case_no = $("input[name='case_no[]']:checked").map(function () {
                return $(this).val();
            }).get();#}
            {#let username = $("input[name='username[]']:checked").map(function () {
                return $(this).val();
            }).get();#}
            if (ids.length == 0){
                 layer.msg("请勾选要提交的数据", {icon: 2, time: 1000})
                 return false
            }
            $.ajax({
                url: '/admin/record/update',
                data: {ids: ids, idno: idno, sex: sex, test_time: test_time, case_no: case_no, username: username,random:'{{ random }}'},
                dataType: 'json',
                type: 'post',
                success: function (result) {
                    if (result.success) {
                        parent.layer.close(parent.layer.getFrameIndex(window.name))//关闭当前页
                        parent.layui.table.reload('record-table')
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000})
                    }
                }
            })
            return false
        })
        $(".id-search").click(function (e) {
            let id_card = $(this).prev().val();
            let that = this;
            if (id_card == ''){
                alert("请输入身份证号");
                return;
            }
            {#if (!identityValidate(id_card)){
                alert("身份证号格式不正确");
                return;
            }#}
            $.ajax({
                url: '/admin/record/searchId',
                data: JSON.stringify({id_card: id_card}),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.success) {
                        let record = result.data;
                        if (Object.keys(record).length > 0) {
                            $(that).parent().parent().find("input[name='username[]']").val(record.username)
                        }
                        $(that).parent().parent().find(".year").val(IdCard(id_card, 3))
                        let sex = IdCard(id_card, 2);
                        $(that).parent().parent().find("select[name='sex[]'] option").each(function (i, n) {
                            if ($(n).val() == sex) {
                                $(n).attr("selected", true);
                            }
                        })
                        layui.form.render("select");
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000})
                    }
                }
            })
        })
    })
</script>
<script>
</script>
</body>
</html>